class Admin::CouponsController < AdminController
  def index
    @coupons = Coupon.where("shop_id = 0 OR shop_id IS NULL").order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def new
    @coupon = Coupon.new
  end

  def create
    @coupon = Coupon.new coupon_params
    if @coupon.save
      redirect_to admin_coupons_path, notice: "保存成功！"
    else
      redirect_to admin_coupons_path, notice: "保存失败！"
    end
  end

  def edit
    @coupon = Coupon.find params[:id]
  end

  def update
    @coupon = Coupon.find params[:id]
    if @coupon.update coupon_params
      redirect_to admin_coupons_path, notice: "更新成功！"
    else
      redirect_to admin_coupons_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @coupon = Coupon.find params[:id]
    if @coupon.destroy
      redirect_to admin_coupons_path, notice: "删除成功."
    else
      redirect_to admin_coupons_path, notice: "删除失败."
    end
  end

  private

  def coupon_params
    params.require(:coupon).permit(:name, :remark, :shop_category_id, :start_at, :end_at, :money, :full_money, :state)
  end
end
